package com.example.demo;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.function.Predicate;

public class HelloApplication_TableView extends Application {
    //
    // Set up the model which is two lists of Players and a filter criteria
    //
    ReadOnlyObjectProperty<ObservableList<Item>> deptsProperty =
            new SimpleObjectProperty<>(FXCollections.observableArrayList(Item.ITEM_LIST));

    ReadOnlyObjectProperty<FilteredList<Item>> viewableDeptsProperty =
            new SimpleObjectProperty<FilteredList<Item>>(
                    new FilteredList<>(deptsProperty.get()
                    ));

    ObjectProperty<Predicate<? super Item>> filterProperty =
            viewableDeptsProperty.get().predicateProperty();

    @Override
    public void start(Stage primaryStage) throws Exception {

        TableView<Item> tblItems = new TableView<>();

        TableColumn<Item, String> colSKU = new TableColumn<>("SKU");
        TableColumn<Item, String> colDescr = new TableColumn<>("Item");
        TableColumn<Item, Float> colPrice = new TableColumn<>("Price");
        TableColumn<Item, Boolean> colTaxable = new TableColumn<>("Tax");

        tblItems.getColumns().addAll(
                colSKU, colDescr, colPrice, colTaxable
        );

        colSKU.setCellValueFactory( new PropertyValueFactory<>("sku") );
        colDescr.setCellValueFactory( new PropertyValueFactory<>("descr") );
        colPrice.setCellValueFactory( new PropertyValueFactory<>("price") );
        colTaxable.setCellValueFactory( new PropertyValueFactory<>("taxable") );
        Button btnInventory = new Button("Inventory");
        Button btnCalcTax = new Button("Tax");

        btnInventory.disableProperty().bind(
                tblItems.getSelectionModel().selectedItemProperty().isNull()
        );

        btnCalcTax.disableProperty().bind(
                tblItems.getSelectionModel().selectedItemProperty().isNull().or(
                        Bindings.select(
                                tblItems.getSelectionModel().selectedItemProperty(),
                                "taxable"
                        ).isEqualTo(false)
                )
        );

        Button button =new Button("isTax");
        button.setOnAction( event -> {
            filterProperty.set(Item::getTaxable);
        } );


        HBox buttonHBox = new HBox( btnInventory, btnCalcTax ,button );
        buttonHBox.setSpacing( 8 );

        VBox vbox = new VBox( tblItems, buttonHBox );
        vbox.setPadding( new Insets(10) );
        vbox.setSpacing( 10 );

        Scene scene = new Scene(vbox);

        tblItems.setItems(viewableDeptsProperty.get());

        primaryStage.setTitle("TableSelectApp");
        primaryStage.setScene( scene );
        primaryStage.setHeight( 376 );
        primaryStage.setWidth( 667 );
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch();
    }
}